<?php
/***************************************************************
 *  Copyright notice
 *
 *  (c) 2008 Иванова Марина <mariwa@m-design.ru>
 *  All rights reserved
 *
 *  This script is part of the TYPO3 project. The TYPO3 project is
 *  free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  The GNU General Public License can be found at
 *  http://www.gnu.org/copyleft/gpl.html.
 *
 *  This script is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  This copyright notice MUST APPEAR in all copies of the script!
 ***************************************************************/


// DEFAULT initialization of a module [BEGIN]
unset($MCONF);
require_once('conf.php');
require_once($BACK_PATH.'init.php');
require_once($BACK_PATH.'template.php');

$LANG->includeLLFile('EXT:user_farm_import/mod1/locallang.xml');
require_once(PATH_t3lib.'class.t3lib_scbase.php');
$BE_USER->modAccess($MCONF,1);	// This checks permissions and exits if the users has no permission for entry.
// DEFAULT initialization of a module [END]



/**
 * Module 'Import' for the 'user_farm_import' extension.
 *
 * @author	Иванова Марина <mariwa@m-design.ru>
 * @package	TYPO3
 * @subpackage	user_farmimport
 */
class  user_farmimport_module1 extends t3lib_SCbase {
	var $pageinfo;

	/**
	 * Initializes the Module
	 * @return	void
	 */
	function init()	{
		global $BE_USER,$LANG,$BACK_PATH,$TCA_DESCR,$TCA,$CLIENT,$TYPO3_CONF_VARS;

		parent::init();

		/*
		 if (t3lib_div::_GP('clear_all_cache'))	{
		 $this->include_once[] = PATH_t3lib.'class.t3lib_tcemain.php';
		 }
		 */
	}

	/**
	 * Adds items to the ->MOD_MENU array. Used for the function menu selector.
	 *
	 * @return	void
	 */
	function menuConfig()	{
		global $LANG;
		$this->MOD_MENU = Array (
						'function' => Array (
							'1' => $LANG->getLL('uplPrice'),
							'2' => $LANG->getLL('uplProducts'),
							'3' => $LANG->getLL('uplGroups'),
		)
		);
		parent::menuConfig();
	}

	/**
	 * Main function of the module. Write the content to $this->content
	 * If you chose "web" as main module, you will need to consider the $this->id parameter which will contain the uid-number of the page clicked in the page tree
	 *
	 * @return	[type]		...
	 */
	function main()	{
		global $BE_USER,$LANG,$BACK_PATH,$TCA_DESCR,$TCA,$CLIENT,$TYPO3_CONF_VARS;
 

		// Access check!
		// The page will show only if there is a valid page and if this page may be viewed by the user
		/*$this->pageinfo = t3lib_BEfunc::readPageAccess($this->id,$this->perms_clause);
 
		$access = is_array($this->pageinfo) ? 1 : 0;

		if (($this->id && $access) || ($BE_USER->user['admin'] && !$this->id))	{
*/
			// Draw the header.
			$this->doc = t3lib_div::makeInstance('mediumDoc');
			$this->doc->backPath = $BACK_PATH;
			$this->doc->form='<form action="" method="POST">';

			// JavaScript
			$this->doc->JScode = '
							<script language="javascript" type="text/javascript">
								script_ended = 0;
								function jumpToUrl(URL)	{
									document.location = URL;
								}
							</script>
						';
			$this->doc->postCode='
							<script language="javascript" type="text/javascript">
								script_ended = 1;
								if (top.fsMod) top.fsMod.recentIds["web"] = 0;
							</script>
						';

			$headerSection = $this->doc->getHeader('pages',$this->pageinfo,$this->pageinfo['_thePath']).'<br />'.$LANG->sL('LLL:EXT:lang/locallang_core.xml:labels.path').': '.t3lib_div::fixed_lgd_pre($this->pageinfo['_thePath'],50);

			$this->content.=$this->doc->startPage($LANG->getLL('title'));
			$this->content.=$this->doc->header($LANG->getLL('title'));
			$this->content.=$this->doc->spacer(5);
				
			$this->content.=$this->doc->section('',$this->doc->funcMenu(
			$headerSection,t3lib_BEfunc::getFuncMenu($this->id,'SET[function]',$this->MOD_SETTINGS['function'],
			$this->MOD_MENU['function'])));
				
			$this->content.=$this->doc->divider(5);


			// Render content:
			$this->moduleContent();


			// ShortCut
			if ($BE_USER->mayMakeShortcut())	{
				$this->content.=$this->doc->spacer(20).$this->doc->section('',$this->doc->makeShortcutIcon('id',implode(',',array_keys($this->MOD_MENU)),$this->MCONF['name']));
			}
			$this->content.=$this->doc->spacer(10);
		/*} else {
			// If no access or if ID == zero

			$this->doc = t3lib_div::makeInstance('mediumDoc');
			$this->doc->backPath = $BACK_PATH;

			$this->content.=$this->doc->startPage($LANG->getLL('title'));
			$this->content.=$this->doc->header($LANG->getLL('title'));
			$this->content.=$this->doc->spacer(5);
			$this->content.=$this->doc->spacer(10);
		}*/
	}

	/**
	 * Prints out allowed for be_user branches
	 *
	 * @return	void
	 */
	function branch_selector()
	{
		global $BE_USER,$TYPO3_DB;
		
		 //if ($GLOBALS['BE_USER']->isAdmin()) 
		if ($BE_USER->user['admin']){
			$res = $TYPO3_DB->exec_SELECTquery (
					 'name,uid',
					 'user_farm_branches',
					'deleted=0'
					);


		}
		else {
			$res = $GLOBALS['TYPO3_DB']->sql_query("SELECT name,uid
   												FROM user_farm_branches 
   		 	WHERE user_farm_branches.deleted=0 AND group_id IN (".$GLOBALS['BE_USER']->groupList.") 
   		 	OR leader_id = ".$GLOBALS['BE_USER']->user["uid"]);				
				
		}

		$code = '
			<div id="c-select">
				<select name="branch_id"  style="width:100px;">';//onchange="reload(this.options[this.selectedIndex].value);"

		while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))	{
			//	debug($row,'$row', __LINE__, __FILE__);
				
			$code .= '<option  value="'.$row['uid'].'" >'.$row['name'].'</option>';
		}

		$code.='
				</select>
			</div>	';
		return $code;
	}
	/**
	 * Prints out the module HTML
	 *
	 * @return	void
	 */
	function printContent()	{

		$this->content.=$this->doc->endPage();
		echo $this->content;
	}

	/**
	 * Generates the module content
	 *
	 * @return	void
	 */
	function moduleContent()	{
		global $BE_USER,$LANG,$TCA,$TYPO3_DB;
	 //	debug($GLOBALS['TYPO3_CONF_VARS']['SYS']['form_enctype']);

		switch((string)$this->MOD_SETTINGS['function'])	{
			case 1:
			
				$code = '<div align=center><strong>'.$LANG->getLL('uplPrice').'</strong></div>'.'</form>
				<form action="csv_tce_file.php" method="post" name="editform" enctype="multipart/form-data">';
					
				$code.= '<div align="right">'.$LANG->getLL('uplPrice_for_branch').$this->branch_selector().'</div>'
				.$LANG->getLL('uplPrice_invite')
				
				.'<div id="c-upload">	
				<input type="file" name="upload_1" '.$this->doc->formWidth(35).' size="50"  />
				<input type="hidden" name="file[upload][1][data]" value="1" /> 
				<input type="hidden" name="vC1" value="'.$GLOBALS['BE_USER']->veriCode().'" />
					<br />	</div>	
			<div id="c-submit">
				<input type="submit" value="'.$LANG->sL('LLL:EXT:lang/locallang_core.php:file_upload.php.submit',1).'" />
				<input type="submit" value="'.$LANG->sL('LLL:EXT:lang/locallang_core.php:labels.cancel',1).'" onclick="backToList(); return false;" />
				 
			</div>
			</form>
				<form action="tce_file.php" method="post" name="editform" enctype="'.$GLOBALS['TYPO3_CONF_VARS']['SYS']['form_enctype'].'">
			<div id="c-submit">
				<input type="submit" value="'.$LANG->getLL('uplGroups_back').'" />
			 
			</div></form><form action="" method="post">
			
		';
				// Add the HTML as a section:
				$this->content .= $this->doc->section($LANG->getLL('uplPrice'),$code,0,1);

				break;
			case 2:
					
				 
				$code = $LANG->getLL('uplProducts_invite');	// enctype="'.$GLOBALS['TYPO3_CONF_VARS']['SYS']['form_enctype'].'"

				$code .= '</form>
				
				<form action="csv_tce_file.php" method="post" name="editform" enctype="multipart/form-data"> 
				<div id="c-upload">	 
					<input type="file" name="upload_1" '.$this->doc->formWidth(35).' size="50"  />
					<input type="hidden" name="file[upload][1][data]" value="1" /> 
					<input type="hidden" name="vC2" value="'.$GLOBALS['BE_USER']->veriCode().'" />
					<br />
				</div><div id="c-submit">
				
				<input type="submit" value="'.$LANG->sL('LLL:EXT:lang/locallang_core.php:file_upload.php.submit',1).'" />
				<input type="submit" value="'.$LANG->sL('LLL:EXT:lang/locallang_core.php:labels.cancel',1).'" onclick="backToList(); return false;" />
				</div>
				</form>
				<form action="tce_file.php" method="post" name="editform" enctype="'.$GLOBALS['TYPO3_CONF_VARS']['SYS']['form_enctype'].'">';
				$code.='
			<div id="c-submit">
				<input type="submit" value="'.$LANG->getLL('uplGroups_back').'" />
			 
			</div></form><form action="" method="post">
			
		';

				// CSH:
				//	$code.= t3lib_BEfunc::cshItem('xMOD_csh_corebe', 'file_upload', $GLOBALS['BACK_PATH'],'<br/>');

				// Add the HTML as a section:
				$this->content .= $this->doc->section($LANG->getLL('uplProducts'),$code,0,1);

				break;
			case 3:
				$code ='<div align=center><strong>'.$LANG->getLL('uplGroup').'</strong></div>';

				$code .= $LANG->getLL('uplGroups_invite');	// enctype="'.$GLOBALS['TYPO3_CONF_VARS']['SYS']['form_enctype'].'"
				$code .= '</form><form action="csv_tce_file.php" method="post" name="editform" enctype="multipart/form-data">';
					

				// Produce the number of upload-fields needed:
				$code.='
			<div id="c-upload">		';
					
				// Adding 'size="50" ' for the sake of Mozilla!
				$code.='
				<input type="file" name="upload_1" '.$this->doc->formWidth(35).' size="50"  />
				<input type="hidden" name="file[upload][1][data]" value="1" /> 
				<input type="hidden" name="vC3" value="'.$GLOBALS['BE_USER']->veriCode().'" />
					<br />
			';
					
				$code.='			</div>		';

				// Submit button:
				$code.='
			<div id="c-submit">
				<input type="submit" value="'.$LANG->sL('LLL:EXT:lang/locallang_core.php:file_upload.php.submit',1).'" />
				<input type="submit" value="'.$LANG->sL('LLL:EXT:lang/locallang_core.php:labels.cancel',1).'" onclick="backToList(); return false;" />
				 
			</div>
		';
				$code .= '</form>
				<form action="tce_file.php" method="post" name="editform" enctype="'.$GLOBALS['TYPO3_CONF_VARS']['SYS']['form_enctype'].'">';
				$code.='
			<div id="c-submit">
				<input type="submit" value="'.$LANG->getLL('uplGroups_back').'" />
			 
			</div></form><form action="" method="post">
			
		';

				// CSH:
				//	$code.= t3lib_BEfunc::cshItem('xMOD_csh_corebe', 'file_upload', $GLOBALS['BACK_PATH'],'<br/>');

				// Add the HTML as a section:
				$this->content .= $this->doc->section($LANG->getLL('uplGroups'),$code,0,1);

				break;

		}

	}
}



if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/user_farm_import/mod1/index.php'])	{
	include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/user_farm_import/mod1/index.php']);
}




// Make instance:
$SOBE = t3lib_div::makeInstance('user_farmimport_module1');
$SOBE->init();

// Include files?
foreach($SOBE->include_once as $INC_FILE)	include_once($INC_FILE);

$SOBE->main();
$SOBE->printContent();

?>